package com.yiqi.liebang.feature.people.view;


import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.framework.Callback;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.PeopleBo;
import com.yiqi.liebang.entity.bo.SearchFriendBo;
import com.yiqi.liebang.feature.home.view.adapter.FriendsAdapter;
import com.yiqi.liebang.feature.login.view.LoginActivity;
import com.yiqi.liebang.feature.mine.view.UserInfoActivity;
import com.yiqi.liebang.framework.api.HomeApi;
import com.yiqi.liebang.framework.api.UserApi;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.rongcloud.contactcard.Friend;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


public class PeopleFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, OnRefreshListener,
        BaseQuickAdapter.OnItemChildClickListener {

    @BindView(R.id.rv_people)
    RecyclerView mRvPeople;
    @BindView(R.id.view_people)
    LinearLayout mViewPeople;
    FriendsAdapter questionAdapter;
    @BindView(R.id.smartRefesh)
    SmartRefreshLayout mSmartRefesh;
    @BindView(R.id.tv_show_count)
    TextView mTvShowCount;
    Unbinder unbinder;

    private EmptyView mEmptyView;


    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_people;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        initAdapter();

        mSmartRefesh.setEnableLoadMore(false);
        mSmartRefesh.setOnRefreshListener(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (AM.user().isLogin()) {
            getUserCount();
            getData();
        }

    }

    @Override
    protected void initData() {
        if (!AM.user().isLogin()) {
            startActivity(new Intent(getActivity(), LoginActivity.class));
        } else {
            getUserCount();
            getData();
        }
    }

    @Override
    protected void initEvent() {

    }

    private void getData() {
        AM.api().createApiService(HomeApi.class).tuijianFriends()
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<List<SearchFriendBo.UserFriendBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoading(d);
                    }

                    @Override
                    public void onNext(List<SearchFriendBo.UserFriendBean> friendBeans) {
                        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
                        dismissLoading();
                        if (friendBeans != null && friendBeans.size() > 0) {
                            questionAdapter.setNewData(friendBeans);
                        } else {
                            questionAdapter.setEmptyView(mEmptyView.getEmptyView("暂无推荐好友"));
                            questionAdapter.setNewData(null);
                        }
                        addHead(friendBeans);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
                        dismissLoading();
                        questionAdapter.setEmptyView(mEmptyView.getErrorView(e.getMessage()));
                        questionAdapter.setNewData(null);
                    }

                    @Override
                    public void onComplete() {
                        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
                        dismissLoading();
                    }
                });
    }

    private void initAdapter() {
        mEmptyView = new EmptyView(mRvPeople);
        mRvPeople.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvPeople.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        questionAdapter = new FriendsAdapter(true, getActivity());
        questionAdapter.bindToRecyclerView(mRvPeople);
        questionAdapter.setOnItemClickListener(this);
        questionAdapter.setOnItemChildClickListener(this);
        questionAdapter.setCommonCallback(new Callback.CommonCallback() {
            @Override
            public void onSubscribe(Disposable d) {
                showLoading(d);
            }

            @Override
            public void onSuccess(Object result) {
                dismissLoading();
            }

            @Override
            public void onError(Throwable ex) {
                dismissLoading();
            }

            @Override
            public void onComplete() {
                dismissLoading();
            }
        });

    }

    private void addHead(List<SearchFriendBo.UserFriendBean> friendBeans) {
        List<String> strings = new ArrayList<>();
        List<String> strings2 = new ArrayList<>();
        if (friendBeans == null || friendBeans.size() == 0) {
            return;
        }
        for (int i = 0; i < friendBeans.size(); i++) {
            strings.add(friendBeans.get(i).getUserHead());
        }
        if (strings != null && strings.size() > 3) {
            strings2 = strings.subList(0, 3);
        }
        if (mViewPeople!=null){
        mViewPeople.removeAllViews();}
        for (int i = 0; i < strings2.size(); i++) {
            ImageView imageView = new ImageView(getActivity());
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(64, 64);
            layoutParams.setMargins(15, 0, 0, 0);
            imageView.setLayoutParams(layoutParams);
            AM.image().bindToCircleObject(strings2.get(i), imageView);
            mViewPeople.addView(imageView);
        }
    }


    @OnClick({R.id.btn_people_to_friends, R.id.btn_people_to_people})
    public void onViewClicked(View view) {
        if (!AM.user().isLogin()) {
            startActivity(new Intent(getActivity(), LoginActivity.class));
            return;
        }
        switch (view.getId()) {
            case R.id.btn_people_to_friends:
                startActivity(new Intent(getActivity(), FriendsListActivity.class));
                break;
            case R.id.btn_people_to_people:
                startActivity(new Intent(getActivity(), PeopleActivity.class));
                break;
        }
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(getActivity(), UserInfoActivity.class);
        intent.putExtra("is_view_my", false);
        intent.putExtra("visitorUid", questionAdapter.getData().get(position).getUserUid());
        startActivity(intent);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        getData();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

    }

    private void getUserCount() {
        AM.api().createApiService(UserApi.class).getMyFriendList(new PeopleBo())
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer()).subscribe(new Observer<List<Friend>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(List<Friend> friends) {
                if (friends != null&&mTvShowCount!=null)
                    mTvShowCount.setText(friends.size() + "");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

}
